<?php
class Api_Model_Luutam extends Zend_Db_Table_Abstract
{
	protected $_name="Luutam";
	protected $_primary="Id";

	var $Id, $Sohoadon, $Ngaydatmon, $IdBan, $IdMon, $Soluong, $Trangthai, $TrangthaiOrder;

	public function __construct() {
		$this->_name = Zend_Registry::get('tablePrefix') . $this->_name;
		parent::__construct();
	}

	public function Save()
	{
	    $data = array(
    		'Ngaydatmon' => $this->Ngaydatmon,
    		'IdBan' => $this->IdBan,
	    	'IdMon' => $this->IdMon,
	    	'Soluong' => $this->Soluong,
	    	'Trangthai' => $this->Trangthai,
	    	'TrangthaiOrder'=>$this->TrangthaiOrder
	    );
	    
	    $this->insert($data);
	    
	    return $this->getAdapter()->lastInsertId();
	}
	
	
	public function getDsmontheoban()
	{
	    $query = $this->_db->select()
	    	->from(array('T1'=>'os_luutam'))
	    	->joinLeft(array('T2' => 'os_dmhanghoadv'), 'T1.IdMon = T2.Id', array('T2.Ten', 'T2.GiaBan'))
	    	->where("(Ngaydatmon BETWEEN '". date('Y-m-d 00:00:00') ."' And '". date('Y-m-d 23:59:59') ."') And IdBan = '$this->IdBan'")
	    	->where("T1.Trangthai = 'chuathanhtoan'");
	    	
	    $stmt = $this->_db->query($query);
	    
	   	return $result = $stmt->fetchAll();
	}
	
	public function getDsmon($_tungay, $_denngay,$ngaydatmon)
	{	 
	   	$query = $this->_db->select()
	   	->from(array('T1'=>'os_luutam'))
	   	->joinLeft(array('T2' => 'os_dmban'), 'T1.IdBan = T2.Id', array('T2.Ten as TenBan'))
	   	->joinLeft(array('T3' => 'os_dmhanghoadv'), 'T1.IdMon = T3.Id', array('T3.Ten as TenMon'))
	   	->where("T1.Ngaydatmon Between '$_tungay' And '$_denngay'")
	   	->where("T1.TrangthaiOrder = 'daxacnhan'");	   
	    
	   	$stmt = $this->_db->query($query);	   	
	   	return $stmt->fetchAll();
	}
	
	
	public function deleteOderById()
	{
	    return $this->delete("Id = '$this->Id'");
	}
	
	public function cancelOder($IdBan)
	{
		return $this->delete("IdBan = '$IdBan' and  Trangthai = 'chuathanhtoan' and Ngaydatmon BETWEEN '". date('Y-m-d 00:00:00') ."' and '". date('Y-m-d 23:59:59') ."'");		
	}
	
	public function getdsbanactive()
	{
	    $query = $this->_db->select()
	    				->from(array('T1'=>'os_luutam'), "IdBan")
	    				->where("Ngaydatmon BETWEEN '". date('Y-m-d 00:00:00') ."' and '". date('Y-m-d 23:59:59') ."'  and TrangThai = 'chuathanhtoan'")
	    				->group("IdBan");
	    
	    $stmt = $this->_db->query($query);
	    return $result = $stmt->fetchAll();
	}
	
	//Thuan
	public function UpdateSohoadon($lstIdLuutamDachon='(0)')
	{
	    $data = array(
	    		'Sohoadon' => $this->Sohoadon,
	    		'Trangthai' => "dathanhtoan"
	    );
	    
	    $this->update($data, "Ngaydatmon BETWEEN '". date('Y-m-d 00:00:00') ."' and '". date('Y-m-d 23:59:59') ."'  and IdBan = '$this->IdBan' And Trangthai = 'chuathanhtoan' And Id In $lstIdLuutamDachon");
	}
	
	public function UpdateTrangthaiOrder($lstId,$kiemtra,$Id)
	{
		if($kiemtra == 1){
		    $data = array(
		    		'TrangthaiOrder' => "daxacnhan");
			$this->update($data,"Id In $lstId");
		}
		
		if($kiemtra == 0){
		    $data_ = array(
		    		'TrangthaiOrder' => "daxacnhan");
		    $this->update($data_,"Id = '$Id'");
		}
		
		if($kiemtra == -1){
			$data__ = array(
					'TrangthaiOrder' => "dathuchien");
			$this->update($data__,"Id = '$Id'");
		}
	   
	}
	
	public function xoaluutamBySohoadon($Sohoadon)
	{
		return $this->delete("Sohoadon = '$Sohoadon'");
	}
	
	public function chuyenban($lstIdban)
	{
		$data = array(
				'IdBan' => $this->IdBan				
		);
		 
		$this->update($data,"Id In $lstIdban");		
	}
}